home *** CD-ROM | disk | FTP | other *** search
- Path: qualcomm.com!usenet
- From: nabbasi@qualcomm.com (Nasser Abbasi)
- Newsgroups: comp.lang.c++
- Subject: Re: Asynchronous I/O, Re: cin.get() function challenge
- Date: 1 Feb 1996 01:21:27 GMT
- Organization: QUALCOMM
- Message-ID: <4ep4in$a31@qualcomm.com>
- References: <4eavds$d0u@news.cencom.net> <ALUN.CHAMPION.96Jan29143951@g7240065.bridge.bst.bls.com> <bredelin-3101961313580001@apm-b325-8.ucsd.edu>
- NNTP-Posting-Host: nabbasi.qualcomm.com
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.93.14
-
- In article <bredelin-3101961313580001@apm-b325-8.ucsd.edu>,
- bredelin@sdcc13.ucsd.edu says...
- >
- >I've been trying to find a read routine that doesn't block (i.e. wait to
- >return) until a carriage return is entered. My program needs to
- >continuously process data, stopping periodically to process any input
- that
- >may have enterred the keyboard buffers since that last time it checked.
- >
- >
- >A. On method would be the check the size of the keyboard buffer, called
- >cin.get() only if a complete line is present. That would be optimal,
- and
- >would not involve any blocking. However, whoever wrote iostream went to
- >great lengths to make sure that checking the size of cin's buffer is
- >really nasty. I think I would have to write my own io library just to
- >check the size of the keyboard buffer!
- >
-
- well, Assuming you are on an environment that supports real threads,
- there is a much simpler way to do this than writing an IO library !
-
- Create a thread that its job in life is to read complete lines
- from the keyboard, let it block as it wants. When the input is
- complete, it queues the input to another thread.
-
- Now you are out of the realm of the IO stream stuff, and can use
- system API calls to do inter-threads interface stuff. which in
- system with threads (such as WIN95, NT, Solaris, OS/2, etc..) these
- system services are avaliable.
-
- What you then have is a client thread that reads input, have a server
- thread that loops processing input from its input queue. the client
- thread writes lines of data (messages) into the server input queue, and
- signal the server thread to wake , if the server thread has nothing else
- to do, it might be sleep waiting for data to arrive and will wake up, if
- it has work to do, next time it checks its input queue, it will find
- data there, and will not sleep (no harm of the signal being lost then).
-
- This way yout program can do usefull work, and at the same time part
- of it is blocked waiting for a complete line to be typed in, which is
- what you want to accomplish.
-
- So, all what you need is a thread-safe QUEUE class, a semophore signaling
- mechanism (system API), and couple of little threads you create from the
- main(), something like 50-100 lines of C++ code and and you are all
- set !
-
- Nasser
-
-